package com.guanyu.u2cache.controller;


import com.alibaba.fastjson.JSON;
import com.guanyu.u2cache.api.U2CacheLevel2Api;
import com.guanyu.u2cache.domain.Result;
import com.guanyu.u2cache.domain.Teacher;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("/cache")
public class CacheOperateController {

    @Resource
    private U2CacheLevel2Api u2CacheLevel2Api;


  //  @ApiOperation(value = "list")
    @GetMapping("/list/")
    public Result list() throws Exception {
        log.info("开始测试list");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        List tea = u2CacheLevel2Api.putListByKeyExpire("tea", teachers, 10, TimeUnit.SECONDS);
        List searchTea = u2CacheLevel2Api.findListByKey("tea");
        log.info("查询结果:" + JSON.toJSONString(searchTea));
        u2CacheLevel2Api.deleteInfoByKey("tea");
        log.info("删除后查询结果:" + JSON.toJSONString(u2CacheLevel2Api.findListByKey("tea")));
        return Result.success();
    }

  //  @ApiOperation(value = "listMap")
    @GetMapping("/listMap/")
    public Result listMap() throws Exception {
        log.info("开始测试listMap");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        Map<String, List<Teacher>> map = new HashMap<>();
        map.put("teaMap", teachers);
        u2CacheLevel2Api.putAllList("tea1", map, 10, TimeUnit.SECONDS);
        Map tea1 = u2CacheLevel2Api.findAllMapByKey("tea1");
        log.info("数据信息map:" + JSON.toJSONString(tea1));
        Object tea2 = u2CacheLevel2Api.findMapByKey("tea1", "teaMap");
        log.info("数据信息list:" + JSON.toJSONString(tea2));
        u2CacheLevel2Api.deleteByKey("tea1");
        log.info("删除后数据信息:" + JSON.toJSONString(u2CacheLevel2Api.findAllMapByKey("tea1")));
        return Result.success();
    }

}
